<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @itemClick="itemClick" ref="nav"></detail-nav-bar>
    <scroll class="content"
            :probe-type="3"
            ref="scroll"
            @scroll="contentScroll">
      <detail-swiper :top-image="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shop="shop"></detail-shop-info>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <detail-param-info :param-info="paramInfo" ref="params"></detail-param-info>
      <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
    <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
  </div>
</template>

<script>
  import DetailNavBar from './childComps/DetailNavBar'
  import DetailSwiper from './childComps/DetailSwiper'
  import DetailBaseInfo from './childComps/DetailBaseInfo'
  import DetailShopInfo from './childComps/DetailShopInfo'
  import DetailGoodsInfo from './childComps/DetailGoodsInfo'
  import DetailParamInfo from './childComps/DetailParamInfo'
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailBottomBar from './childComps/DetailBottomBar'

  import GoodsList from 'components/content/goods/GoodsList'

  import Scroll from 'components/common/scroll/Scroll'

  import {getDetail,Goods,Shop,GoodsParams,getRecommend} from "network/detail";

  import {debounce} from "common/utils";
  import {backTopMixin} from "common/mixin";

  import {mapActions} from 'vuex'

  export default {
    name: "Detail",
    components:{
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailBottomBar,
      GoodsList,
      Scroll
    },
    mixins:[backTopMixin],
    data(){
      return{
        iid:null,
        topImages:[],
        goods:{},          //商品信息
        shop:{},           //店铺信息
        detailInfo:{},    //商品细节
        paramInfo: {},    //参数信息
        commentInfo: {},  //评论信息
        recommends:[],    //推荐信息
        themTopYs:[],
        getTopY:null,
        currentIndex:0
      }
    },
    methods: {
      ...mapActions(['addCart']),
      itemClick(index) {
        this.$refs.scroll.scrollTo(0, -this.themTopYs[index] , 200)
      },

      imageLoad() {
        this.getTopY()
      },

      contentScroll(position) {
        const positionY = -position.y
        const length = this.themTopYs.length;
        for (let i = 0; i < length - 1; i++) {
          if (i !== this.currentIndex && (positionY >= this.themTopYs[i] && positionY < this.themTopYs[i + 1])) {
            this.currentIndex = i;
            this.$refs.nav.currentIndex = this.currentIndex;
          }
        }
        this.listenShowBackTop(position)
      },

      addToCart(){
        const product={}
        product.image=this.topImages[0]
        product.title=this.goods.title
        product.desc=this.goods.desc
        product.price=this.goods.realPrice
        product.iid=this.iid

        if(product.image){
          this.addCart(product).then(res=>{
            this.$toast.show(res)
          })
        }else{
          this.$toast.show('添加购物车失败')
        }
      }
    },

    created(){
      this.iid=this.$route.params.iid

      getDetail(this.iid).then(res=> {
        const data = res.result

        this.topImages = data.itemInfo.topImages

        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        this.shop = new Shop(data.shopInfo)

        this.detailInfo = data.detailInfo

        this.paramInfo = new GoodsParams(data.itemParams.info, data.itemParams.rule)

        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }
      })

      getRecommend().then(res => {
        this.recommends = res.data.list
      })
    },
    mounted(){
      this.getTopY = debounce(() => {
        this.themTopYs = [];
        this.themTopYs.push(0);
        this.themTopYs.push(this.$refs.params.$el.offsetTop);
        this.themTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themTopYs.push(this.$refs.recommend.$el.offsetTop);
        this.themTopYs.push(Number.MAX_VALUE);
      }, 200);
    }
  }
</script>

<style scoped>
  #detail{
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }

  .detail-nav{
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

  .content{
    position: relative;
    height: calc(100% - 44px);
  }
</style>
